using System;
using System.Collections.Generic;
using Castle.Components.Validator;
using Castle.MonoRail.Framework;
using Rhino.Commons;
using WineCellar.Data;
using WineCellar.Web.Filters;

namespace WineCellar.Web.Controllers
{
    [Layout("index"), AuthenticationFilter, Rescue("generalerror")]
    public class RaisinController : ApplicationController
    {
        public void List()
        {
            if (IsAjaxRequest) { CancelLayout(); }
            ICollection<Raisin> raisins = Repository<Raisin>.FindAll();
            PropertyBag["raisins"] = raisins;            
        }
        public void New()
        {
            PropertyBag["raisin"] = new Raisin();
        }
        public void Create([DataBind("raisin", Validate = true)]Raisin raisin)
        {
            if (!HasValidationError(raisin))
            {
                raisin.AddedBy = GetLoginUser();
                raisin.DateAdded = DateTime.Now;
            	Repository<Raisin>.Save(raisin);                
                PropertyBag["updatedObject"] = raisin;
            }
            else
            {
                Flash["updatedObject"] = raisin;
                Flash["summary"] = GetErrorSummary(raisin);                
            }
            RenderView("shared", "create");
        }
        public void Edit(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
            Raisin raisin = Repository<Raisin>.Load(id);
            PropertyBag["raisin"] = raisin;            
        }       
        public void View(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
			PropertyBag["raisin"] = Repository<Raisin>.Load(id);
        }
        public void Update([DataBind("raisin", Validate = true)] Raisin raisin)
        {
            if (!HasValidationError(raisin))
            {
            	With.Transaction(delegate
            	                 	{
										Repository<Raisin>.SaveOrUpdate(raisin);				
            	                 	});				
                PropertyBag["updatedObject"] = raisin;
            }
            else
            {
                Flash["updatedObject"] = raisin;
                Flash["summary"] = GetErrorSummary(raisin);                
            }
            RenderView("shared", "update");
        }
        public void Delete(int id)
        {
			Raisin raisin = Repository<Raisin>.Load(id);
            if (raisin.Wines.Count <= 0)
            {
                With.Transaction(delegate
                                 	{
										Repository<Raisin>.Delete(raisin);	
                                 	});				
                PropertyBag["deletedObject"] = raisin;
            }
            else
            {
                ErrorSummary summary = new ErrorSummary();
                summary.RegisterErrorMessage("Cellar", "Unable to delete raisin, wines are already created that use the raisin");
                Flash["summary"] = summary;
                Flash["deletedObject"] = raisin;
            }
            RenderView("shared", "delete");
        }
    }
}
